package com.chinatelecom.otms.circuitcheck;

import com.alibaba.fastjson.JSON;
import com.chinatelecom.otms.circuitcheck.entity.Snc;
import com.chinatelecom.otms.circuitcheck.entity.User;
import com.chinatelecom.otms.circuitcheck.runnable.CheckCircuitRunnable;
import com.chinatelecom.otms.circuitcheck.dao.CheckCircuitDao;
import com.chinatelecom.otms.circuitcheck.entity.CheckCircuit;
import com.chinatelecom.otms.circuitcheck.service.CheckCircuitService;
import com.chinatelecom.otms.circuitcheck.util.Common;
import com.chinatelecom.otms.circuitcheck.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;

@Component
public class CheckCircuitRun implements ApplicationRunner {

    @Autowired
    private CheckCircuitService checkDataService;

    @Autowired
    private CheckCircuitDao checkCircuitDao;

    public CheckCircuitRun() {
    }

    public void testMessage(Object obj){
        String s = obj.toString();
        System.out.println(s);
    }

    @Autowired
    private KafkaTemplate kafkaTemplate;

    private static String IS_SNC_READ_BY_FILE = System.getenv("is_snc_read_by_file");
    private static String SNC_FILE_PATH = System.getenv("snc_file_path");
    private static String CIRCUIT_FILE_PATH = System.getenv("circuit_file_path");
    private static String IS_SNC_OR_FDFR = System.getenv("is_snc_or_fdfr");  // snc 代表读取的文件是SNC, fdfr 代表读取的文件是FDFR

    static {
        Common.TABLE_NAME = System.getenv("table_name");
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {

        System.out.println("hello kepler");
        List<User> list = checkCircuitDao.selectUser();

        for(User user : list){
            System.out.println(user.toString());
        }

        List<CheckCircuit> checkCircuitsList = getSncTheStateNot5AndGetRoute(IS_SNC_READ_BY_FILE);
        for(CheckCircuit c : checkCircuitsList){
            System.out.println(c.toString());
        }
//        checkCircuitsList = checkDataService.checkAZPTPContrastResultAndUpdate(checkCircuitsList);
//        checkCircuitsList = checkDataService.checkCarrierAndSncRoute(checkCircuitsList);
//        checkCircuitsList = checkDataService.checkFDFRinCarrier(checkCircuitsList);
//        checkCircuitsList = checkDataService.checkMFDFRinPort(checkCircuitsList, false);
//        checkCircuitsList = checkDataService.checkMFDFRinPort(checkCircuitsList, true);
//        checkCircuitsList = checkDataService.checkFDFRRouteInSncAndMFDFR(checkCircuitsList);

//        List<String> list = checkDataService.checkL2LinkRoute(checkCircuitsList);
//        for(String s : list){
//            System.out.println(s);
//        }

//        checkCircuitsList = checkDataService.checkPortStateVlan(checkCircuitsList);

//        List<String> list = checkDataService.checkBindingRel(checkCircuitsList);
//        for(String s : list){
//            System.out.println(s);
//        }

//        List<String> list = checkDataService.checkFTPBindingRel(checkCircuitsList);
//        for(String s : list){
//            System.out.println(s);
//        }

//        checkDataService.truncateTableAndInsertChechCircuit(checkCircuitsList, true);
    }

//    private static void povit() throws IOException {
//        String fileName = "C:\\Users\\kepler\\Desktop\\povit.txt";
//
//        String[] tenantId_args = {"1001", "1002", "1003"};
//        String[] userId_args = {"u01", "u02", "u03", "u04"};
//        String[] spuId_args = {"spu001", "spu002", "spu003", "spu004"};
//        String[] eventCode_args = {"goodsCart", "goodsView", "goodsFavorite", "goodsRate", "orderTrade"};
//        int[] score_args = {0, 1, 2, 3};
//
//        for(int i = 0; i < 100; i ++){
//            int tenantId_num = new Random().nextInt(3);
//            int userId_num = new Random().nextInt(4);
//            int spuId_num = new Random().nextInt(4);
//            int eventCode_num = new Random().nextInt(5);
//            int score_num = new Random().nextInt(4);
//
//            Povit povit = new Povit(tenantId_args[tenantId_num], userId_args[userId_num],
//                    spuId_args[spuId_num], eventCode_args[eventCode_num], score_args[score_num]);
//
//            FileUtil.writeLine(JSON.toJSONString(povit) + "\n", fileName);
//        }
//
//        System.out.println("over");
//    }

    private List<CheckCircuit> getSncTheStateNot5AndGetRoute(String isSncRefReadByFile) throws InterruptedException {
        List<CheckCircuit> checkCircuitList = new ArrayList<>();
        if(isSncRefReadByFile.equals("true")){
            String[] snc_args = FileUtil.readStringListByFile(SNC_FILE_PATH).split(",");
            for(String snc : snc_args){
                checkCircuitList.add(new CheckCircuit(snc));
            }
            checkSncRefRunnable(checkCircuitList);
        }else{
            checkCircuitList = getCheckCircuitRefByCircuitFile(CIRCUIT_FILE_PATH);
            checkSncRefRunnable(checkCircuitList);
        }
        return checkCircuitList;
    }

    public List<CheckCircuit> getCheckCircuitRefByCircuitFile(String circuit_file_path){
        String circuit_string = FileUtil.readStringListByFile(circuit_file_path);
        String[] circuit_string_args = circuit_string.split(",");
        List<String> tempList = new ArrayList<>();
        for(String circuit_name : circuit_string_args){
            tempList.add(circuit_name);
        }
        List<CheckCircuit> checkCircuitList = checkCircuitDao.executeConnectByRootSql(tempList);
        return checkCircuitList;
    }

    public void checkSncRefRunnable(List<CheckCircuit> checkCircuitsList) throws InterruptedException {
        checkCircuitsList = checkDataService.getEmsIdByCheckCircuitList(checkCircuitsList);
        Common.countDownLatch = new CountDownLatch(checkCircuitsList.size());
        for(int i = 0; i < checkCircuitsList.size(); i ++){
            Common.singleThreadExecutor.execute(new CheckCircuitRunnable(checkCircuitsList.get(i), checkDataService, i, checkCircuitsList.size(), IS_SNC_OR_FDFR));
        }
        Common.countDownLatch.await();
    }
}
